package top.itaso.app;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import top.itaso.app.room.entity.UserEntity;
import top.itaso.lib.widget.tpv.TpvCommonItemView;

/**
 * @Description: 登录页面的已离线注册过的用户列表适配器
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Gitee https://gitee.com/itsdf07
 * @Date 2022/1/11
 */
public class UserHistoryAdapter extends RecyclerView.Adapter<UserHistoryAdapter.ItemViewHolder> {
    private List<UserEntity> mUsersDatas = new ArrayList<>();

    private IUserChooseCallback mUserChooseCallback;

    public interface IUserChooseCallback {
        void onItemUser(UserEntity user);

        void onLongItemUser(UserEntity user);
    }

    public void setUserChooseCallback(IUserChooseCallback callback) {
        this.mUserChooseCallback = callback;
    }

    public void refreshUsers(List<UserEntity> users) {
        mUsersDatas.clear();
        mUsersDatas.addAll(users);
        notifyDataSetChanged();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_user_exist_item, parent, false);
        TpvCommonItemView commonItemView = new TpvCommonItemView(parent.getContext());
        commonItemView.setLayoutVisibility(25);

        return new ItemViewHolder(commonItemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        TpvCommonItemView commonItemView = (TpvCommonItemView) holder.itemView;
        UserEntity user = mUsersDatas.get(position);
        commonItemView.setTitleContent(user.getUserName());
        String subTitle = TextUtils.isEmpty(user.getDisplayName()) ? "暂无昵称" : user.getDisplayName() + "      " + user.getCreateDate();
        commonItemView.setSubTitleContent(subTitle);
        commonItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUserChooseCallback != null) {
                    mUserChooseCallback.onItemUser(user);
                }
            }
        });
        commonItemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mUserChooseCallback != null) {
                    mUserChooseCallback.onLongItemUser(user);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mUsersDatas.size();
    }

    protected class ItemViewHolder extends RecyclerView.ViewHolder {

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
